<template>
	<view class="home">
		<!-- 搜索组件 -->
		<view class="searchcom">
			<my-search @click="toSearch" bgc = "#c00000" :radius="50"></my-search>
		</view>
		<view class="main">


			<!-- 轮播图 -->
			<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
				:circular="true">
				<swiper-item v-for="(item, index) in swiperList" :key="index">
					<navigator :url="item.navigator_url">
						<image class="topimg" :src="item.image_src"></image>
					</navigator>
				</swiper-item>
			</swiper>
			<!-- 分类导航 -->
			<view class="nav">
				<view @click="toCate(index)" class="item" v-for="(item,index) in cateList" :key="index">
					<image :src="item.image_src" mode="widthFix"></image>
				</view>
			</view>
			<!-- 楼层 -->
			<view class="floor">
				<!-- 每个楼层 -->
				<view class="item" v-for="(item,index) in floorList" :key="index">
					<!-- 标题区域 -->
					<view class="title">
						<image :src="item.floor_title.image_src" mode="widthFix"></image>
					</view>
					<!-- 图片区域 -->
					<view class="imgbox">
						<view class="left">
							<image @click="toList(item.product_list[0].name)" :src="item.product_list[0].image_src"
								:style="{ width: `${item.product_list[0].image_width}rpx` }" mode="widthFix">
							</image>
						</view>
						<view class="right">
							<template v-for="(subitem, subindex) in item.product_list">
								<image @click="toList(subitem.name)" v-if="subindex !== 0" mode="widthFix"
									:style="{ width: `${subitem.image_width}rpx` }" :src="subitem.image_src"
									:key="subindex"></image>
							</template>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mymixins from '../../mixin/index.js'
	export default {
		mixins: [mymixins],
		data() {
			return {
				// 轮播图数据源
				swiperList: [],
				// 分类导航数据源
				cateList: [],
				// 楼层数据源
				floorList: []
			};
		},
		onLoad() {
			// 获取轮播图数据源
			this.getSwiperList()
			this.getCateList()
			this.getFloorList()
		},
		methods: {
			// 获取楼层数据源
			async getFloorList() {
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()

				this.floorList = res.message

			},
			// 获取轮播图数据源
			async getSwiperList() {
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				if (res.meta.status !== 200) return uni.$showMsg()

				this.swiperList = res.message
			},
			// 获取分类导航的数据源
			async getCateList() {
				// 将接口返回的对象中的 data 属性解构出来，再给它设置一个别名为 res
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				// 判断状态
				if (res.meta.status !== 200) return uni.$showMsg()
				// 保存数据源
				this.cateList = res.message
			},
			// 跳转到分类页
			toCate(index) {
				// 判断是否点击的是分类
				if (index === 0) {
					// 跳转到分类页面
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 跳转到商品列表页面
			toList(name) {
				uni.navigateTo({
					url: `/pages/list/list?key=${name}`
				})
			},
			// 搜索跳转点击事件
			toSearch(){
				uni.navigateTo({
					url:'/pages/search/search'
				})
			}

		}

	}
</script>

<style lang="scss">
	.searchcom {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 999;
	}

	.main {
		position: relative;

		.swiper {
			height: 360rpx;

			.topimg {
				width: 100%;
				height: 360rpx;
			}
		}

		.nav {
			padding: 20rpx 0;
			display: flex;

			.item {
				width: 25%;
				text-align: center;

				image {
					width: 70%;
				}
			}
		}

		.floor {
			.item {
				.title {
					image {
						width: 100%;
					}
				}

				.imgbox {
					display: flex;
					margin: 0 10rpx;

					.left {
						margin-right: 10rpx;
					}

					.right {
						image {

							&:nth-child(1),
							&:nth-child(3) {
								margin-right: 10rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
